﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI;
using System.Web.UI.HtmlControls;

using BlueCMS.Core.Entities;
using BlueCMS.Core.Services;

namespace BlueCMS.Core.Pages
{
    public class PageletRegion : Control, INamingContainer
    {
        PageletInstance mPageletInstance { get; set; }
        bool mIsEditMode { get; set; }

        public PageletRegion(PageletInstance pageletInstance, bool isEditMode)
        {
            mPageletInstance = pageletInstance;
            mIsEditMode = isEditMode;
        }

        protected override void CreateChildControls()
        {
            HtmlGenericControl div = new HtmlGenericControl("div");
            Controls.Add(div);

            if (mIsEditMode && UserService.UserHasPermissionForNode(CmsApplication.CmsUser, "NODE", "EDIT", mPageletInstance.Node))
            {
                // Add toolbar for pagelet instance
                PageletRegionToolbarBase control = (PageletRegionToolbarBase)Page.LoadControl("~/_System/Controls/PageletRegionToolbar.ascx");
                control.PageletInstance = mPageletInstance;
                div.Controls.Add(control);
                div.Attributes.Add("class", "BCMS-PageletRegion-EditMode");
            }
            else
            {
                div.Attributes.Add("class", "BCMS-PageletRegion-DisplayMode");
            }

            if (UserService.UserHasPermissionForNode(CmsApplication.CmsUser, "NODE", "VIEW", mPageletInstance.Node))
            {
                Control control = Page.LoadControl(String.Format("~/_System/Pagelets/{0}", mPageletInstance.Pagelet.control_nm));

                if (control != null)
                {
                    div.Controls.Add(control);
                }
            }
        }
    }
}
